﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.Specialized;

using System.Data;
using System.Data.OleDb;

using Common;

using Castle.Core.Interceptor;
using Castle.DynamicProxy;

namespace com.globalwave.a8ext
{
    public class OrderService : BaseService
    {
        private static NameValueCollection whereMap = null;
        private const string TABLE_NAME = "Order";

        
        public OrderService()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        public OrderService(BaseService other)
            : base(other) 
        {
        }

        virtual public int save(NameValueCollection order, NameValueCollection[] details) {

            NameValueCollection where = new NameValueCollection() ;
            where.Add("order_number", order["order_number"]) ;

            if (dao.queryTable(TABLE_NAME, where).Tables[0].Rows.Count > 0) {
                throw new Exception("订单编号已经存在！");
            }

            int orderId = dao.insertTable(TABLE_NAME, order);

            for (int i = 0; i < details.Length; i ++ )
            {
                NameValueCollection detail = details[i];
                detail.Add("order_id", orderId.ToString());
                dao.insertTable("order_detail", detail);

            }

            return orderId;
        }

        virtual public int create(NameValueCollection product)
        {
            return dao.insertTable(TABLE_NAME, product);
        }

        virtual public int update(NameValueCollection order)
        {
            NameValueCollection criterion = new NameValueCollection();
            string id = order.Get("order_id");

            criterion.Add("order_id", id);

            order.Remove("order_id");

            int result = dao.updateTable(TABLE_NAME, order, criterion);

            order.Add("order_id", id);

            return result;
        }

        virtual public int delete(NameValueCollection criterion)
        {
            if (criterion.Get("order_id") != null)
            {
                throw new BaseException("E000001", "参数错误");
            }
            // delete cascade 
            //dao.deleteTable(TABLE_BULLETIN, criterion);
            //dao.deleteTable(TABLE_TOPIC, criterion);

            return dao.deleteTable(TABLE_NAME, criterion);

        }

        virtual public void deleteAll(string[] productIds)
        {
            if (productIds == null || productIds.Length == 0)
            {
                throw new BaseException("", "parames");
            }

            NameValueCollection criterion = new NameValueCollection();
            foreach (string productId in productIds)
            {
                criterion.Add("order_ids", productId);
            }

            // delete cascade
            //dao.deleteTable(TABLE_BULLETIN, criterion);
            //dao.deleteTable(TABLE_TOPIC, criterion);

            dao.deleteTable(TABLE_NAME, criterion);

        }

        virtual public int clearHistory(DateTime systemDate)
        {

            OleDbParameter[] paramss = new OleDbParameter[] { new OleDbParameter("@process_date", systemDate.GetType()) };
            paramss[0].Value = systemDate.ToShortDateString();

            //select * from order_detail where order_id in( select order_id from [order] where order_date<=#2011-2-4#)
            this.dao.execute("delete from order_detail where order_id in (select order_id from [order] where process_date<>?)", paramss, false);

            return this.dao.execute("delete from [order] where process_date<>?", paramss, false);

        }

        virtual public string getNextOrderNumber(DateTime date)
        {
            //long orderId = getMaxOrderId(date);

            NameValueCollection criterion = new NameValueCollection();
            criterion.Add("process_date", date.ToShortDateString());

            long orderId = getMaxOrderId(criterion);

            if (orderId == -1) {
                return "0";
            }

            //NameValueCollection criterion = new NameValueCollection();

            criterion.Add("order_id", orderId.ToString());

            string result = this.dao.getString("SELECT order_number FROM [Order] ", criterion);

            int len = result.Length;
            return (long.Parse(result) + 1).ToString().PadLeft(len, '0');


            /*

            NameValueCollection criterion = new NameValueCollection();

            criterion.Add("process_date", date.ToShortDateString());

            string result = this.dao.getString("SELECT max(order_number) FROM [Order] ", criterion);

            if (result == null)
            {
                //return ((long)((date.Year * 100 + date.Month) * 100 + date.Day)) * 10000 + 1;
                return "0";
            }
            else
            {
                int len = result.Length;
                return (long.Parse(result) + 1).ToString().PadLeft(len, '0');
            }
            */
        }


        virtual public long getMaxOrderId(DateTime date)
        {

            NameValueCollection criterion = new NameValueCollection();

            criterion.Add("process_date", date.ToShortDateString());

            //return this.dao.getLong("SELECT max(order_id) FROM [Order] ", criterion, -1);
            return getMaxOrderId(criterion);

        }

        public long getMaxOrderId(NameValueCollection criterion) {
            return this.dao.getLong("SELECT max(order_id) FROM [Order] ", criterion, -1);
        }

        virtual public string getString(string sql,NameValueCollection criterion)
        {
            return this.dao.getString(sql, criterion);
        }

        public NameValueCollection getWhereConfigMap()
        {
            if (whereMap != null)
            {
                return whereMap;
            }

            whereMap = new NameValueCollection();

            whereMap.Add("order_ids", " order_id in (@order_ids) ");
            //whereMap.Add("FullName", " FullName like ? ");
            //whereMap.Add("UserCode", " UserCode like ? ");

            return whereMap;
        }

        public static OrderService getInstance()
        {

            ProxyGenerator generator = new ProxyGenerator();

            return generator.CreateClassProxy<OrderService>(new TransactionInterceptor());

        }
    }
}
